package com.ruoyi.system.controller;

import java.net.URLDecoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.html.HTMLFilter;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAlarmConfig;
import com.ruoyi.system.service.ISysAlarmConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 告警配置Controller
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/system/alarm/config")
public class SysAlarmConfigController extends BaseController
{
    @Autowired
    private ISysAlarmConfigService sysAlarmConfigService;

    @Autowired
    private ISysDeviceService sysDeviceService;
    /**
     * 查询告警配置列表
     */
    @PreAuthorize("@ss.hasPermi('config:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAlarmConfig sysAlarmConfig)
    {
        startPage();
        sysAlarmConfig.setDeptId(getDeptId());
        List<SysAlarmConfig> list = sysAlarmConfigService.selectSysAlarmConfigList(sysAlarmConfig);
        return getDataTable(list);
    }

    /**
     * 导出告警配置列表
     */
    @PreAuthorize("@ss.hasPermi('config:config:export')")
    @Log(title = "告警配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAlarmConfig sysAlarmConfig)
    {
        sysAlarmConfig.setDeptId(getDeptId());
        List<SysAlarmConfig> list = sysAlarmConfigService.selectSysAlarmConfigList(sysAlarmConfig);
        ExcelUtil<SysAlarmConfig> util = new ExcelUtil<SysAlarmConfig>(SysAlarmConfig.class);
        util.exportExcel(response, list, "告警配置数据");
    }

    /**
     * 获取告警配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('config:config:query')")
    @GetMapping(value = "/{alarmConfigId}")
    public AjaxResult getInfo(@PathVariable("alarmConfigId") Long alarmConfigId)
    {
        SysAlarmConfig config = sysAlarmConfigService.selectSysAlarmConfigByAlarmConfigId(alarmConfigId);
        List <SysDevice> deviceList = sysDeviceService.selectSysDeviceList(SysDevice.builder().alarmConfigId(alarmConfigId).build());
        config.setDeviceList(deviceList);
        return success(config);
    }

    /**
     * 新增告警配置
     */
    @PreAuthorize("@ss.hasPermi('config:config:add')")
    @Log(title = "告警配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAlarmConfig sysAlarmConfig)
    {
        sysAlarmConfig.setDeptId(getDeptId());
        return toAjax(sysAlarmConfigService.insertSysAlarmConfig(sysAlarmConfig));
    }

    /**
     * 修改告警配置
     */
    @PreAuthorize("@ss.hasPermi('config:config:edit')")
    @Log(title = "告警配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAlarmConfig sysAlarmConfig)
    {
        return toAjax(sysAlarmConfigService.updateSysAlarmConfig(sysAlarmConfig));
    }

    /**
     * 删除告警配置
     */
    @PreAuthorize("@ss.hasPermi('config:config:remove')")
    @Log(title = "告警配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{alarmConfigId}")
    public AjaxResult remove(@PathVariable Long alarmConfigId)
    {
        return toAjax(sysAlarmConfigService.deleteSysAlarmConfigByAlarmConfigId(alarmConfigId));
    }
}
